import argparse
import json
import _mongo
from flask import Flask, request, make_response

import process

parser = argparse.ArgumentParser()
parser.add_argument('-P')

args = parser.parse_args()

if args.P:
    RUN1_PORT = int(args.P)
else:
    RUN1_PORT = 5001

app = Flask(__name__)


@app.route("/index", methods=["get"])
def index():
    process_dic = process.get_process()

    # print(process_dic)

    resp = make_response(json.dumps(process_dic))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/acvsFaceFace", methods=["get"])
def acvsFaceFace():
    acvsid = request.args.get("acvsid")
    faceid = request.args.get("faceid")
    face_list = _mongo.acvs_face_face(acvsid, faceid)

    print(face_list)  # db15的所有信息
    print(len(face_list))

    resp = make_response(json.dumps(face_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/acvsAcvsFace", methods=["get"])
def acvsAcvsFace():
    acvs1 = request.args.get("acvs1")
    acvs2 = request.args.get("acvs2")
    face_list = _mongo.acvs_acvs_face(acvs1, acvs2)

    true_list = []
    for img in face_list:
        if img['is_front'] == 'True':
            true_list.append(img)
    print(len(true_list))

    print(face_list)  # db15的所有信息
    print(len(face_list))
    resp = make_response(json.dumps(face_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/allCapture", methods=["get"])
def allCapture():
    page = int(request.args.get("page"))
    pagesize = int(request.args.get("pagesize"))
    print(page, pagesize)

    face_list = _mongo.get_all_face_capture()

    face_list = face_list[page * pagesize: page * pagesize + pagesize]
    # print('p_list', p_list)   # 档案id中的 所有脸的id
    # print(acvs_list)

    print(face_list)  # db15的所有信息
    resp = make_response(json.dumps(face_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/acvsAll", methods=["get"])
def acvsAll():
    page = int(request.args.get("page"))
    pagesize = int(request.args.get("pagesize"))
    print(page, pagesize)
    value_list = _mongo.get_acvs_id()  # 获取档内档案列表中放元组

    print("Archives length:", len(value_list))

    value_list = value_list[page * pagesize: page * pagesize + pagesize]

    # ls = [str(v[0], encoding="UTF-8") for v in value_list]
    # 圈外档案id
    id_out = []
    ls = []
    for item in value_list:
        ls.append(
            {
                "id": item[0],
                "len1": item[1],
                "len2": item[2],
                "len3": item[3],
                "acvslen": item[4] + item[5] + item[6],
                "p_id": item[8],  # face_id
                "url": item[7],  # 第一张图片的face_url
                'lencard': item[9]
            }
        )

    print('ls-----------', ls)  # 存放字典
    resp = make_response(json.dumps(ls))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/acvsInfo", methods=["get"])
def acvsInfo():
    id = request.args.get("id")
    print("id", id)  # 档案id
    p_list = _mongo.getfaceacvs(id)
    acvs_list = _mongo.acvsacvs(id)
    card_list = _mongo.cardList(id)
    # print('p_list', p_list)   # 档案id中的 所有脸的id
    # print(acvs_list)
    pic_list = []
    for acvsfollow in acvs_list:
        img = _mongo.acvsshow(id, acvsfollow)
        pic_list.append(img)

    pic_list.append({})

    for cardimg in card_list:
        pic_list.append(cardimg)

    pic_list.append({})
    for p in p_list:
        pic_list.append(p)

    # print(pic_list)  # db15的所有信息
    resp = make_response(json.dumps(pic_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/cardAll", methods=["get"])
def cardAll():
    page = int(request.args.get("page"))
    pagesize = int(request.args.get("pagesize"))
    print(page, pagesize)
    value_list = _mongo.getcardlist()
    print("card length:", len(value_list))

    value_list = value_list[page * pagesize: page * pagesize + pagesize]
    ls = []
    for item in value_list:
        ls.append(
            {
                "id": item['_id'],
                "card_len": item['lenface'],
                "p_id": item['_id'],  # face_id
                "url": item['face_image_uri'],  # 第一张图片的face_url
                "name": item['name'],
                "person_id": item['person_id'],
                "face_id": item['_id'],
                "acvslen": item['lenacvs']
            }
        )

    # print('ls-----------', ls) # 存放字典
    resp = make_response(json.dumps(ls))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/cardInfo", methods=["get"])
def cardInfo():
    id = request.args.get("id")
    print("id", id)  # card_id
    face_list = _mongo.gatallfaceid(id)
    acvs_list = _mongo.getallacvsid(id)
    # print(face_list)
    # print(acvs_list)
    # print('p_list', p_list)   # card_id中的 所有脸的id
    pic_list = []

    img = _mongo.getcardimgg(id)
    pic_list.append(img)

    pic_list.append({})

    for acvsid_sim in acvs_list:
        acvsid = acvsid_sim[0]
        img = _mongo.cardacvsshow(acvsid, id)
        pic_list.append(img)

    pic_list.append({})

    for otherface in face_list:
        pic_list.append(otherface)

    # print(pic_list)
    # print(pic_list)  # db15的所有信息
    resp = make_response(json.dumps(pic_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


#  新增档档树关系

@app.route("/acvsP", methods=["get"])
def cardP():
    page = int(request.args.get("page"))
    pagesize = int(request.args.get("pagesize"))
    print(page, pagesize)
    value_list = _mongo.get_p_list()
    print("acvsp length:", len(value_list))

    value_list = value_list[page * pagesize: page * pagesize + pagesize]
    ls = []
    for item in value_list:
        ls.append(
            {
                "id": item['_id'],
                "p_id": item['_id'],  # face_id
                "url": item['face_image_uri'],  # 第一张图片的face_url
                "face_id": item['f_id']
            }
        )

    # print('ls-----------', ls) # 存放字典
    resp = make_response(json.dumps(ls))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/acvsSub", methods=["get"])
def cardSub():
    id = request.args.get("id")
    print("id", id)  # card_id
    all_acvs_list = _mongo.one_get_all_acvs(id)
    # print(face_list)
    # print(acvs_list)
    # print('p_list', p_list)   # card_id中的 所有脸的id
    pic_list = []

    for sub_acvs in all_acvs_list:
        pic_list.append(sub_acvs)

    print(pic_list)
    # print(pic_list)  # db15的所有信息
    resp = make_response(json.dumps(pic_list))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


@app.route("/ovoImg", methods=["get"])
def ovoImg():
    page = int(request.args.get("page"))
    pagesize = int(request.args.get("pagesize"))
    print(page, pagesize)
    value_list = _mongo.get_ovo_img()  # 获取档内档案列表中放元组

    print("ovo length:", len(value_list))

    value_list = value_list[page * pagesize: page * pagesize + pagesize]

    ls = []
    for item in value_list:
        ls.append(
            {
                "id": 1,
                "yituovo": item['yituovo'],
                "ovo": item['ovo'],
                "p_id": 1,  # face_id
                "url": item['url'],  # 第一张图片的face_url
                'ovn': item['ovn']
            }
        )
    print(ls)
    # print('ls-----------', ls) # 存放字典
    resp = make_response(json.dumps(ls))
    resp.headers["Access-Control-Allow-Origin"] = "*"
    resp.headers["Access-Control-Allow-Methods"] = "GET,POST"
    resp.headers["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
    return resp


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=RUN1_PORT)
